package ru.arriah.servicedesk.web.leadExecutor.action;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class ProcessTaskReportFormAction extends DumbAction {
  protected RequestManagerLocalHome requestManagerLocalHome;    
  
  public ProcessTaskReportFormAction () throws NamingException {
    super();
    
    requestManagerLocalHome = 
      Utils.getRequestManagerHomeInterface();
  }

  public String execute(ActionMapping actionMapping,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    try {     
     
     RequestManagerLocalObject requestManager = 
       requestManagerLocalHome.create();
               
     int taskId = idVal(request.getParameter("task_id"));
     String requestReport = request.getParameter("text");
     
     if (requestManager.setTaskReport(taskId, requestReport)) 
       addInfMessage(
          request,
          config.getProperty("INF.TASKREPORT_ADDED")
       );
     else 
        addErrorMessage(
           request,
           config.getProperty("ERROR.TASKREPORT_NOT_ADDED")
        );
     
     TaskBean taskBean = requestManager.selectTask(taskId);
     
     request.setAttribute("taskBean", taskBean);
     
     requestManager.remove();   
     return actionMapping.getSuccessTarget();
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}